package com.dc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.annotation.Log;
import com.dc.common.ResponseResultVO;
import com.dc.entity.ro.DeviceRO;
import com.dc.enums.SystemClass;
import com.dc.model.Device;
import com.dc.service.DeviceService;
import com.dc.util.RedisUtil;
import com.dc.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "设备信息管理")
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @Log(title = "设备信息管理", businessDesc = "新增设备")
    @ApiOperation(value = "新增设备", notes = "新增设备")
    @PostMapping("/insertDevice")
    public ResponseResultVO insertDevice(@RequestBody DeviceRO ro) {
        return deviceService.insertDevice(ro);
    }

    @Log(title = "设备信息管理", businessDesc = "修改设备")
    @ApiOperation(value = "修改设备", notes = "修改设备")
    @PostMapping("/updateDevice")
    public ResponseResultVO updateDevice(@RequestBody DeviceRO ro) {
        return deviceService.updateDevice(ro);
    }

    @Log(title = "设备信息管理", businessDesc = "替换设备")
    @ApiOperation(value = "替换设备", notes = "替换设备")
    @PostMapping("/replaceDevice")
    public ResponseResultVO replaceDevice(@RequestBody DeviceRO ro) {
        return deviceService.replaceDevice(ro);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/deleteDevice")
    public ResponseResultVO deleteDevice(@RequestParam String id) {
        return deviceService.deleteDevice(id);
    }

    @ApiOperation(value = "查询列表", notes = "查询列表")
    @PostMapping("/selectDeviceByPage")
    public ResponseResultVO selectDeviceByPage(@RequestBody DeviceRO ro) {
        return deviceService.selectDeviceByPage(ro);
    }

    @ApiOperation(value = "大屏查询列表", notes = "大屏查询列表")
    @GetMapping("/selectDeviceScreen")
    public ResponseResultVO selectDeviceScreen(DeviceRO ro) {
        return deviceService.selectDeviceByPage(ro);
    }

    @ApiOperation(value = "列表导出", notes = "列表导出")
    @PostMapping("/deviceRecordExport")
    public void deviceRecordExport(HttpServletResponse response, @RequestBody DeviceRO ro) {
        deviceService.deviceRecordExport(response, ro);
    }

    @ApiOperation(value = "设备记录模板下载", notes = "设备记录模板下载")
    @RequestMapping(value = "/exportDeviceRecordModel", method = RequestMethod.POST)
    public void exportDeviceRecordModel(HttpServletResponse response) {
        deviceService.exportDeviceRecordModel(response);
    }

    @ApiOperation(value = "设备记录模板导入", notes = "设备记录模板导入")
    @RequestMapping(value = "/importDeviceRecord", method = RequestMethod.POST)
    public ResponseResultVO importDeviceRecord(@RequestParam("file") MultipartFile file) {
        return deviceService.importDeviceRecord(file);
    }

    @ApiOperation(value = "文件内容读取", notes = "文件内容读取")
    @RequestMapping(value = "/fileContentReading", method = RequestMethod.POST)
    public ResponseResultVO fileContentReading(@RequestParam("file") MultipartFile file) {
        return deviceService.fileContentReading(file);
    }

    @ApiOperation(value = "设备类型", notes = "设备类型")
    @RequestMapping(value = "/getDeviceType", method = RequestMethod.GET)
    public ResponseResultVO getDeviceType() {
        return deviceService.getDeviceType();
    }

    @ApiOperation(value = "楼层获取", notes = "楼层")
    @RequestMapping(value = "/getDeviceFloor", method = RequestMethod.GET)
    public ResponseResultVO getDeviceFloor() {
        Set<Map<String, Object>> list = new HashSet<>();
        deviceService.list(new LambdaQueryWrapper<Device>().select(Device::getFloorCode)
                .and(wrapper -> {
                    wrapper.ne(Device::getFloorCode, StringUtils.EMPTY)
                            .isNotNull(Device::getFloorCode);
                }).orderByAsc(Device::getFloorCode))
                .stream().forEach(cv -> {
            Map<String, Object> collect = new HashMap<>(1);
            collect.put("name", cv.getFloorCode());
            list.add(collect);
        });
        return ResponseResultVO.successResult(list);
    }

    @ApiOperation(value = "所属子系统", notes = "所属子系统")
    @RequestMapping(value = "/getSubsystem", method = RequestMethod.GET)
    public ResponseResultVO getSubsystem() {
        List<Map<String, String>> list = new ArrayList<>();
        Arrays.stream(SystemClass.values()).forEach(cc -> {
            Map<String, String> collect = new HashMap<>(1);
            collect.put("name", cc.getName());
            list.add(collect);
        });
        return ResponseResultVO.successResult(list);
    }

    /**
     * 获取摄像头设备信息
     *
     * @param page 页码
     * @return
     */
    @ApiOperation(value = "获取摄像头设备信息", notes = "获取摄像头设备信息")
    @RequestMapping(value = "/splDevice/getDeviceInfo", method = RequestMethod.GET)
    public ResponseResultVO getDeviceInfo(@RequestParam(name = "page", required = true) int page) {
        return deviceService.getDeviceInfo(page);
    }

    /**
     * 获取摄像头实时视频地址
     *
     * @param deviceCode 摄像机编码
     * @return
     */
    @Log(title = "火警处置工单管理", businessDesc = "获取摄像头实时视频地址")
    @ApiOperation(value = "获取摄像头实时视频地址", notes = "获取摄像头实时视频地址")
    @RequestMapping(value = "/splDevice/getGetVideoRealtime", method = RequestMethod.GET)
    public ResponseResultVO getGetVideoRealtime(@RequestParam(name = "deviceCode", required = true) String deviceCode) {
        return deviceService.getGetVideoRealtime(deviceCode);
    }

    /**
     * 获取摄像头某时间段历史视频地址
     *
     * @param deviceCode 摄像机编码
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return
     */
    @Log(title = "火警处置工单管理", businessDesc = "获取摄像头某时间段历史视频地址")
    @ApiOperation(value = "获取摄像头某时间段历史视频地址", notes = "获取摄像头某时间段历史视频地址")
    @RequestMapping(value = "/splDevice/getDeviceHistory", method = RequestMethod.GET)
    public ResponseResultVO getDeviceHistory(@RequestParam(name = "deviceCode", required = true) String deviceCode,
                                             @RequestParam(name = "startTime", required = true) String startTime,
                                             @RequestParam(name = "endTime", required = true) String endTime) {
        return deviceService.getDeviceHistory(deviceCode, startTime, endTime);
    }

    @PostMapping("/insertDeviceOther")
    public ResponseResultVO insertDeviceOther(@RequestBody Device device) {
        Device serviceOne = deviceService.getOne(new LambdaQueryWrapper<Device>()
                .eq(Device::getSerialNumber, device.getSerialNumber()));
        if (serviceOne == null) {
            DeviceRO deviceRO = new DeviceRO();
            BeanUtils.copyProperties(device, deviceRO);

            // 添加记录到工单中
            return deviceService.insertDevice(deviceRO);
        } else {
            device.setId(serviceOne.getId());
            BeanUtils.copyProperties(device, serviceOne);
            return ResponseResultVO.successResult(deviceService.updateById(device));
        }
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation(value = "test11111111111", notes = "test11111111111")
    @RequestMapping(value = "/test11111111111", method = RequestMethod.GET)
    public Object fileContentReading(String aa, BigDecimal dd) {
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(redisUtil.get("totalArrears:" + 1)));
        return bigDecimal;
    }


}
